package com.powerteam.controller.sys;

import com.github.pagehelper.PageInfo;
import com.powerteam.controller.AuthorizedController;
import com.powerteam.model.sys.Role;
import com.powerteam.model.sys.RoleUser;
import com.powerteam.model.sys.User;
import com.powerteam.service.sys.RoleService;
import com.powerteam.vo.Result;
import com.powerteam.vo.sys.QueryRoleUserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * 角色管理页面相关接口
 *
 * @author jinming.zhang
 * @version 0.0.1
 * @date 2021/12/30
 */
@Slf4j
@Controller
@RequestMapping("/role")
public class RoleController extends AuthorizedController {

    @Autowired
    private RoleService roleService;

    /**
     * 角色管理页
     *
     * @param
     * @return String
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String role() {

        return "sys/role";
    }

    /**
     * 查询所有角色
     *
     * @param
     * @return List<Role>
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @ResponseBody
    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    public List<Role> findAll() {

        return roleService.findAll();
    }

    /**
     * 检查角色名称是否重复
     *
     * @param role
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @ResponseBody
    @RequestMapping(value = "/checkRoleName", method = RequestMethod.POST)
    public Result existRoleName(@RequestBody Role role) {

        return roleService.checkRoleName(role);
    }

    /**
     * 新增角色
     *
     * @param role
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody Role role) {

        return roleService.insert(role);
    }

    /**
     * 批量删除角色
     *
     * @param ids
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @ResponseBody
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Result delete(@RequestBody List<Integer> ids) {

        return roleService.deleteByIds(ids);
    }

    /**
     * 根据角色Id获取角色信息
     *
     * @param role
     * @return Role
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @ResponseBody
    @RequestMapping(value = "/findById", method = RequestMethod.POST)
    public Role findById(@RequestBody Role role) {

        return roleService.findById(role.getRoleId());
    }

    /**
     * 更新角色
     *
     * @param role
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody Role role) {

        return roleService.update(role);
    }

    /**
     * 分配用户
     *
     * @param roleId
     * @return ModelAndView
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @RequestMapping(value = "/user/{roleId}", method = RequestMethod.GET)
    public ModelAndView roleUser(@PathVariable int roleId) {

        ModelAndView vm = new ModelAndView("sys/roleUser");
        vm.addObject("roleId", roleId);
        return vm;
    }

    /**
     * 查询在某角色中的用户列表(分页)
     *
     * @param vo
     * @return PageInfo<User>
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @ResponseBody
    @RequestMapping(value = "/user/findUserInRole", method = RequestMethod.POST)
    public PageInfo<User> findUserInRole(@RequestBody QueryRoleUserVO vo) {

        return roleService.findUserInRole(vo);
    }

    /**
     * 把用户从某角色中移除
     *
     * @param roleUser
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @ResponseBody
    @RequestMapping(value = "/user/remove", method = RequestMethod.POST)
    public Result deleteRoleUser(@RequestBody RoleUser roleUser) {

        return roleService.deleteRoleUser(roleUser);
    }

    /**
     * 查询还未加入到某角色中的用户列表(分页)
     *
     * @param vo
     * @return PageInfo<User>
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @ResponseBody
    @RequestMapping(value = "/user/findUserNotInRole", method = RequestMethod.POST)
    public PageInfo<User> findUserNotInRole(@RequestBody QueryRoleUserVO vo) {

        return roleService.findUserNotInRole(vo);
    }

    /**
     * 分配某用户到某角色中
     *
     * @param roleUser
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @ResponseBody
    @RequestMapping(value = "/user/add", method = RequestMethod.POST)
    public Result addRoleUser(@RequestBody RoleUser roleUser) {

        return roleService.insertRoleUser(roleUser);
    }

    /**
     * 分配菜单
     *
     * @param roleId
     * @return ModelAndView
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @RequestMapping(value = "/menu/{roleId}", method = RequestMethod.GET)
    public ModelAndView roleMenu(@PathVariable Integer roleId) {

        ModelAndView mv = new ModelAndView("sys/roleMenu");
        mv.addObject("roleId", roleId);
        return mv;
    }

    /**
     * 批量分配菜单到某角色中
     *
     * @param map
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @ResponseBody
    @RequestMapping(value = "/menu/saveMenu", method = RequestMethod.POST)
    public Result saveMenu(@RequestBody Map<Object, Object> map) {

        return roleService.saveRoleMenu(map.get("roleId"), map.get("menuIdList"));
    }

}
